home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / MISC / KINGV3.ZIP / XMS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-11  |  6.1 KB  |  216 lines

  1. //XMSEXAM.C   / EXAMPLE FOR FADE XMS ROUTINES//
  2. //WRITING BY THE KING IN 01/02/95            //
  3. #include "KMAGUNIT.C"
  4.  
  5. //This is a struct for move block via XMS to Conventional Memory//
  6. //Or Conventional Memory To XMS//
  7.  
  8. struct XmsMoveStruct {
  9.         long Length;               //The LENGTH of the block in BYTES
  10.         unsigned int SHandle;      //Source Handle
  11.         long SOffset;              //Source Offset
  12.         unsigned int DHandle;      //Destinaton Handle
  13.         long DOffset;              //Destinaton Offset
  14. };
  15.  
  16.  
  17. XmsMoveStruct XMove;     //An Xms Move Stuct , for moving
  18. unsigned long Xms_Address;        //The Address to the software interrupt
  19. unsigned int Handle;     //Handle for allocate memory
  20. char ch;
  21. //-----------------------------------------------//
  22. //       Checking If XMS Program Installed       //
  23. //-----------------------------------------------//
  24.  
  25. char XMSInstalled()
  26. {
  27.     char Installed;
  28.     asm {
  29.     Mov ax,0x4300    //Function Ax=4300, Int 2Fh , Checking XMS INSTALLED
  30.     Int 0x2f
  31.     Cmp al,0x80      //If Al=80h mean that XMS found.
  32.     je Found
  33.     Mov Installed,0 //Else Installed = False
  34.     Jmp Exit       //Exit
  35.     }
  36. Found:asm {
  37.     Mov Installed,1 //In Case XMS INSTALLED , Installed = True
  38.     }
  39.  
  40. Exit:{
  41. }
  42.     return(Installed);  //Return to function result//
  43. }
  44.  
  45. //-----------------------------------------------//
  46. //     Get the XMS software intterupt handle.    //
  47. //-----------------------------------------------//
  48.  
  49. void Get_XMS_Address()
  50. {
  51.     asm {
  52.         Mov Ax,0x4310    //Function Ax=4310h , Int 2Fh , Get Handle Of Xms
  53.         Int 0x2f          //Interrupt.
  54.         Mov Word Ptr [Xms_Address+2],Es  //Moving to The Hi Word Of Handle
  55.                                          //The Hi Word of the Interrupt.
  56.         mov word ptr [Xms_Address],Bx    //Moving to The Low Word Of Handle
  57.                                          //The Low Word of the Interrupt.
  58.     }
  59. }
  60.  
  61. //-----------------------------------------------//
  62. //            Allocate Memory to handle.         //
  63. //-----------------------------------------------//
  64. unsigned char AllocateMemory(unsigned int &Handle,int AllocK)
  65. {
  66. char rett;
  67. asm {
  68.     mov ah,0x09            //Function Ah=09 , Allocate Memory
  69.     Mov Dx,AllocK         //Dx=the size to allocate in KB
  70.     Call Xms_Address      //Calling the interrupt
  71.     Les Di,Handle         //[Es:Di] = Handle
  72.     Mov [Es:Di],Dx        //[Es:Di] = Allocated Handle
  73.     Mov rett,Al
  74.     }
  75.     return(rett);
  76. }
  77.  
  78. //-----------------------------------------------//
  79. //            Free Memory From handle.           //
  80. //-----------------------------------------------//
  81.  
  82. char FreeMemory(int Handle)
  83. {
  84. char rett;
  85. asm {
  86.     Mov Ah,0x0A       //Function Ah=0Ah , Free Memory
  87.     Mov Dx,Handle     //Dx=Handle
  88.     Call Xms_Address  //Calling Intterupt
  89.     mov rett,al
  90.     }
  91.     return(rett);
  92. }
  93.  
  94. //-----------------------------------------------//
  95. //    Reallocated Memory Without losing data     //
  96. //-----------------------------------------------//
  97.  
  98. char ReAllocMemory(int Handle,int NewSize)
  99. {
  100. char rett;
  101. asm {
  102.     Mov Ah,0x0F       //Function Ah=0Fh , Reallocate memory
  103.     Mov Bx,NewSize    //Bx = New Size
  104.     Mov Dx,Handle     //Dx = Handle
  105.     Call Xms_Address  //Calling Interrupt
  106.     mov rett,al
  107.     }
  108.     return(rett);
  109. }
  110.  
  111. //-----------------------------------------------//
  112. // Moving Memory Form Convertional memory To XMS //
  113. //-----------------------------------------------//
  114.  
  115. char RealToXms(long Size,long Source,int DHandle)
  116. {
  117.     char Ok;
  118.     //Building Struct//
  119.     XMove.Length = Size;
  120.     XMove.SHandle = 0;
  121.     XMove.SOffset = Source;
  122.     XMove.DHandle = DHandle;
  123.     XMove.DOffset = 0;
  124.     asm {
  125.         Mov Ah,0x0B      //Function Ah=0Bh , Moving Memory
  126.         Lea SI,XMove     //[Ds:Si] = XMove
  127.         Call Xms_Address //Calling Interrupt
  128.         Mov Ok,Al
  129.     }
  130.     return(Ok);
  131. }
  132.  
  133. //-----------------------------------------------//
  134. // Moving Memory Form XMS To Convertional memory //
  135. //-----------------------------------------------//
  136.  
  137. char XmsToReal(long Size,int SHandle,long Dest)
  138. {
  139. char Ok;
  140.  
  141.     XMove.Length  = Size;
  142.     XMove.SHandle = SHandle;
  143.     XMove.SOffset = 0;
  144.     XMove.DHandle = 0;
  145.     XMove.DOffset = Dest;
  146.     asm {
  147.         Mov Ah,0x0B            //Function Ah=0Bh , Moving Memory
  148.         Lea SI,XMove           //[Ds:Si] = XMove
  149.         Call Xms_Address       //Calling Interrupt
  150.         Mov Ok,Al
  151.     }
  152.     return(Ok);
  153. }
  154. //---------------------------------------------//
  155. //           Return Free XMS Memory            //
  156. //---------------------------------------------//
  157.  
  158. unsigned int FreeMem()
  159. {
  160. unsigned int rett;
  161. asm {
  162.     Mov Ah,0x8           //Ah = 08h , Return Free Memory
  163.     Call Xms_Address     //Calling Interrupt
  164.     mov rett,ax
  165.     }
  166.     return(rett);
  167. }
  168.  
  169. void main() {
  170.  
  171.     if (XMSInstalled()==TRUE)
  172.     {
  173.         printf("XMS Found.\n");
  174.         Get_XMS_Address();
  175.         printf("Total free memory = %d Kb\n",FreeMem());
  176.         printf("\n");
  177.         printf("Press Enter to try to Move the screen memory to XMS\n");
  178.         getch();
  179.  
  180.         if ((AllocateMemory(Handle,8)) == FALSE)
  181.         {
  182.             printf("Cant Allocate 8k of memory in XMS\n");
  183.             exit(1);
  184.         }
  185.  
  186.         if (RealToXms(8000,(long)MK_FP(0x0b800,0),Handle) == FALSE)
  187.         {
  188.             printf("Cant Move From Convertsional memory to XMS.\n");
  189.             exit(1);
  190.         }
  191.  
  192.          clrscr();
  193.  
  194.          printf("Memory Moved Successfull..\n");
  195.          printf("Press Enter To Restore the memory screen from XMS..\n");
  196.          getch();
  197.  
  198.          if (XmsToReal(8000,Handle,(long)MK_FP(0x0b800,0)) == FALSE) {
  199.  
  200.             printf("Cant Move From XMS To Convertsional memory.");
  201.             exit(1);
  202.          }
  203.          if (FreeMemory(Handle)==FALSE)
  204.          {
  205.             printf("Cant Free Memory...\n");
  206.             exit(1);
  207.          }
  208.          gotoxy(1,24);
  209.          printf("Well everything is ok ! :) Press enter to return.\n");
  210.          getch();
  211.  
  212.     }
  213.     else
  214.         printf("XMS Not Found.");
  215.  
  216. }